/**
 * \file Listorifier.h
 *
 * Asteriskos Compiler 1
 * Written 2010-2011, by Anthony Berlin and Alexander Carlqvist
 *
 * This file is part of ICC.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LISTORIFIER_H
#define LISTORIFIER_H

#include <GCB/Object.h>

#include "Tree.h"
#include "SymbolTable.h"
#include "Module.h"

#define EXPR_TREE_TYPES \
    TREE_ASSIGN_EXPR: \
    case TREE_CASTING_EXPR: \
    case TREE_ARITHM_EXPR: \
    case TREE_EQUALITY_EXPR: \
    case TREE_FUNC_CALL: \
    case TREE_IDENT: \
    case TREE_ID_EXPR: \
    case TREE_LITERAL: \
    case TREE_LOGICAL_AND_EXPR: \
    case TREE_LOGICAL_NOT_EXPR: \
    case TREE_LOGICAL_OR_EXPR: \
    case TREE_POLARITY_EXPR: \
    case TREE_PRIMARY_EXPR: \
    case TREE_RELATIONAL_EXPR

///
/// \class Listorifier
/// \brief This class converts an Asteriskos AST to a LISTOR \c Tree and generates LISTOR code.
///
class Listorifier : public Object
{
public:
    /// Constructors.
    Listorifier(list<Module*>& moduleList, Tree* ast, SymbolTable* globalScope,
                Tree* mainDeclTree);
    Listorifier();

    /// Rearrange the AST so that it represents a LISTOR tree.
    void rearrange_ast();

    /// Generate code from the LISTOR \c Tree.
    void generate_code(Tree* codeBlock, bool forceBraces = false);

    /// Walk an Asteriskos scope \c Tree and convert it to a LISTOR \c Tree.
    void walk_cmp_stmt(Tree* asteriskosTree, Tree* listorTree);

    /// Build a LISTOR statement \c Tree from an Asteriskos statement.
    Tree* build_stmt(Tree* tree);

    /// Build a LISTOR expression \c Tree from an Asteriskos expression.
    Tree* build_expr(Tree* expr);

    /// Convert an Asteriskos \c Datatype to a LISTOR datatype represented as a \c string.
    string convert_datatype_instance_to_listor_datatype_string(Datatype* datatype);

    /// Convert an Asteriskos \c parameter \c Tree to a LISTOR parameter represented
    /// as a \c string.
    string convert_param_tree_to_listor_string(Tree* paramTree);

    /// Returns a string of tabs, for the indentation of the LISTOR code (based on the value of
    /// \c m_indentDepth).
    string indentation();

    /// Return the LISTOR \c Tree.
    Tree* get_listor_tree();

    /// Output the generated code to a file.
    void output_code(string path);

    /// Convert this \c Listorifier to a \c string.
    string to_string() const;

    /// Convert this \c Listorifier to a string of \c char*.
    char* to_chars() const;

    /// Print a text version of this \c Listorifier.
    void print() const;

private:
    /// List of all imported modules.
    list<Module*> m_moduleList;

    /// The AST to convert to LISTOR.
    Tree* m_ast;

    /// The global scope of the AST.
    SymbolTable* m_globalScope;

    /// The LISTOR \c Tree created from the AST.
    Tree* m_listorTree;

    /// A list of \c strings representing the output file.
    list<string> m_outputLines;

    /// Current indent depth.
    unsigned long int m_indentDepth;

    /// The main declaration \c Tree, if one exists.
    Tree* m_mainDeclTree;
};

#endif // LISTORIFIER_H
